<?php
namespace app\index\controller;
use think\Request;
use think\Session;
use think\Db;
use think\Controller;

class Book extends Controller
{
	//查看第一次点击
    public function select($id)
    {
        $list = db('chapter')->where('id',$id)->find();
        $res4=db('category')->where('pid',0)->field('id,cname')->select();
        $res=db('txt')->where('id',$list['bid'])->field('name')->find();
        // var_dump($res);die;
        // var_dump($list);die;
        return view('book/book',['data4'=>$res4,'list'=>$list,'res'=>$res]);
    }

    public function shang($id)
    {
    	  // if (!Request::instance()->isAjax()) {
       //      $this->error('你四不四迷路了!', '/');
       //  }
        $lis = db('chapter')->where('id',$id)->find();
        $list = db('chapter')->where('id','<',$id)->order('id','desc')->where('bid',$lis['bid'])->find();
        $res4=db('category')->where('pid',0)->field('id,cname')->select();
        $res=db('txt')->where('id',$list['bid'])->field('name')->find();
          // $list = db('chapter')->where('id','<',$id)->order('id','asc')->find();
        // var_dump($list);die;
          if(!$list){
            $this->error('该书第一章');
        }
        return view('book/book',['data4'=>$res4,'list'=>$list,'res'=>$res]);
    	// return json($list);
    }

    public function xia($id)
    {


          //  }
        $lis = db('chapter')->where('id',$id)->find();
        $list = db('chapter')->where('id','gt',$id)->where('bid',$lis['bid'])->find();
        $res4=db('category')->where('pid',0)->field('id,cname')->select();
        $res=db('txt')->where('id',$list['bid'])->field('name')->find();
          // $list = db('chapter')->where('id','<',$id)->order('id','asc')->find();
        // var_dump($list);die;
        if(!$list){
            $this->error('该书最后一章');
        }
        return view('book/book',['data4'=>$res4,'list'=>$list,'res'=>$res]);
     //    // return json($list);
    	//   if (!Request::instance()->isAjax()) {
     //        $this->error('你四不四迷路了!', '/');
     //    }
     //     $lis = db('chapter')->where('id',$id)->find();
     //    $list = db('chapter')->where('id','gt',$id)->where('bid',$lis['bid'])->find();
     //      // $list = db('chapter')->where('id','<',$id)->order('id','asc')->find();
     //    // var_dump($list);die;
    	// return json($list);
    }

   // 立即阅读
     public function du($bid)
    {
        
    	$list = db('chapter')->where('bid',$bid)->find();
    	$res4=db('category')->where('pid',0)->field('id,cname')->select();
    	$res=db('txt')->where('id',$list['bid'])->field('name')->find();
    	// var_dump($res);die;
    	// var_dump($list);die;
    	if(!$list){$this->error('该书暂时没有章节');};
        return view('book/book',['data4'=>$res4,'list'=>$list,'res'=>$res]);
    }


   // 修改前台资料
    public function info()
    {

        $uid=Session::get('id');
        
        // 一级分类
        $res4=db('category')->where('pid',0)->field('id,cname')->select();
        $data=Db::table('lamp_personshu ps')
            ->where('uid',$uid)
            ->join('lamp_txt t','t.id=ps.sid')
            ->select();
        //友情链接

        $user = db('Huser')->where('id',$uid)->find();
       
        return view('info/add',['data4'=>$res4,'data'=>$data,'user'=>$user]);
      
    }

   // 修改资料
    public function update()
    {
         $id = Session::get('id');
         $date = Db::name('Huser')->field('userpass')->find($id);
        if(input('post.pwd') == null){
            $data = [
                'name' => input('post.name'),
                // 'status' => input('post.status')

            ];
        }else{
            $preg = '/^.{6,20}$/';
                if( !preg_match($preg,input('post.pwd')) ){
                 $this->error('密码长度不够');
                }
                 if(md5(input('post.Jpwd')) != $date['userpass'] ){
                 $this->error('与旧密码不和');
                }
            $data = [
                'name' => input('post.name'),
                // 'status' => input('post.status'),
                'userpass' => md5(input('post.pwd'))
            ];
        }

              // dump($data);exit;
     
       // dump($id);die;
        $result = Db::name('Huser')->where('id',$id)->update($data);

        if ($result) {
            return $this->success('修改成功');
        } else {
            return $this->error('修改失败');
        }
    }
    
}